package com.example.asus.drawstudy.view;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Shader;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.ShapeDrawable;
import android.graphics.drawable.shapes.OvalShape;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

import com.example.asus.drawstudy.R;

/**
 * Created by liangkun on 2018/7/16.
 * 作用:
 */

public class BitmapShaderView extends View {
    private BitmapShader bitmapShader = null;
    private Bitmap bitmap = null;
    private Paint paint = null;
    private ShapeDrawable shapeDrawable = null;
    private int BitmapWidth = 0;
    private int BitmapHeight = 0;

    public BitmapShaderView(Context context) {
        this(context, null);
      //TODO:这个是创建对象用

    }

    public BitmapShaderView(Context context, AttributeSet set) {
        super(context, set);
//        TODO： 默认先走这个
        // 得到图像
        bitmap = ((BitmapDrawable) getResources().getDrawable(R.drawable.cat))
                .getBitmap();
        BitmapWidth = bitmap.getWidth();
        BitmapHeight = bitmap.getHeight();
        Log.e("View", "BitmapWidth " + BitmapWidth + " BitmapHeight " + BitmapHeight);
        // 构造渲染器BitmapShader
        bitmapShader = new BitmapShader(bitmap, Shader.TileMode.MIRROR, Shader.TileMode.MIRROR);
    }


    @Override
    protected void onDraw(Canvas canvas) {
        // TODO Auto-generated method stub
        super.onDraw(canvas);
        //将图片裁剪为椭圆形
        //构建ShapeDrawable对象并定义形状为椭圆
        shapeDrawable = new ShapeDrawable(new OvalShape());
        //得到画笔并设置渲染器
        shapeDrawable.getPaint().setShader(bitmapShader);
        //设置显示区域
        shapeDrawable.setBounds(20, 20, BitmapWidth - 140, BitmapHeight);
        //绘制shapeDrawable
        shapeDrawable.draw(canvas);
    }
}
